package starcoast.bocai.com.starcoast.fragments.usermovable;

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import starcoast.bocai.com.starcoast.R;
import starcoast.bocai.com.starcoast.activitys.movable.PrintMovableActivity;
import starcoast.bocai.com.starcoast.adapter.MyPartMovableAdapter;
import starcoast.bocai.com.starcoast.common.BaseFragment;
import starcoast.bocai.com.starcoast.util.DensityUtil;
import starcoast.bocai.com.starcoast.view.AlertDialog;

/**
 * Created by admin on 2016/6/15.
 */
public class MyPublicMovableFragment extends BaseFragment {
    @Bind(R.id.my_public_lv)
    SwipeMenuListView my_public_lv;
    MyPartMovableAdapter adapter;
    View view;
    private List<ApplicationInfo> mAppList;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my_public, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }
    public void initView() {
        mAppList = getActivity().getPackageManager().getInstalledApplications(0);
        adapter = new MyPartMovableAdapter(getActivity());
        my_public_lv.setAdapter(adapter);
        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        getActivity().getApplicationContext());
                // set item background
                deleteItem.setBackground(R.color.bg_color);
                // set item width
                deleteItem.setWidth(DensityUtil.dip2px(getActivity(),90));
                // set a icon
                deleteItem.setIcon(R.mipmap.delete);
                deleteItem.setTitle("编辑");
                // add to menu
                menu.addMenuItem(deleteItem);
                // create "open" item
                SwipeMenuItem openItem = new SwipeMenuItem(
                        getActivity().getApplicationContext());
                // set item background
                openItem.setBackground(R.color.colorAccent);
                // set item width
                openItem.setWidth(DensityUtil.dip2px(getActivity(), 90));
                // set item title
                openItem.setTitle("删除");
                // set item title fontsize
                openItem.setTitleSize(18);
                // set item title font color
                openItem.setTitleColor(Color.WHITE);
                openItem.setIcon(R.mipmap.delete);
                // add to menu
                menu.addMenuItem(openItem);
            }
        };
        // set creator
        my_public_lv.setMenuCreator(creator);
        my_public_lv.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                ApplicationInfo item = mAppList.get(position);
                switch (index) {
                    case 0:
                        // 编辑
                        startActivity(new Intent(getActivity(), PrintMovableActivity.class));
                        break;
                    case 1:
                        // delete
                        new AlertDialog(getActivity()).builder().setTitle("确认删除吗？")
                                .setPositiveButton("确认", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {

                                    }
                                }).setNegativeButton("取消", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        }).show();
                        break;
                }
                return false;
            }
        });
    }
}
